layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'table', 'laytpl'], function(){
    var $ = layui.$,
        form = layui.form,
        table = layui.table;

    table.render({
        elem: '#data-table',
        url: 'promote/channelList',
        where: getData(),
        toolbar: '#data-table-toolbar',
        title: '推广渠道列表',
        height: 'full-30',
        page: true,
        cols: [[
            {field: 'id', title: 'ID', width: 60, align: 'center'},
            {field: 'code', title: '编号', width: 80, align: 'center'},
            {field: 'username', title: '账号', width: 65, align: 'center'},
            {field: 'type', title: '渠道类型', width: 88, align: 'center', templet: "#typeTpl"},
            {field: 'userId', title: '用户ID', width: 94, align: 'center'},
            {field: 'registerNum', title: '注册人数', width: 60, align: 'center'},
            {field: 'rechagePeopleNum', title: '充值人数', width: 60, align: 'center'},
            {field: 'rechageRate', title: '充值比例', width: 70, align: 'center'},
            {field: 'rechageAmountNum', title: '充值总额', width: 60, align: 'center'},
            {field: 'first_invite_recharge_rate', title: '直接邀请的用户充值金额的奖励比例(%)', width: 156, align: 'center'},
            {field: 'first_invite_rate', title: '直接邀请的主播收入的奖励比例(%)', width: 142, align: 'center'},
            {field: 'second_invite_recharge_rate', title: '间接邀请的用户充值金额的奖励比例(%)', width: 157, align: 'center'},
            {field: 'second_invite_rate', title: '间接邀请的主播收入的奖励比例(%)', width: 142, align: 'center'},
            {field: 'first_invite_vip_rate', title: '直接邀请的用户充值会员的奖励比例(%)', width: 156, align: 'center'},
            {field: 'first_invite_user_rate', title: '直接邀请用户注册奖励的积分', width: 128, align: 'center'},
            {field: 'status', title: '状态', width: 60, align: 'center', templet: "#statusTpl"},
            {field: 'createTime', title: '创建时间', width: 160, align: 'center'},
            {field: 'op', title: '操作', width: 138, align: 'center', toolbar: '#data-table-bar', templet: '#data-table-bar'}
        ]],
        parseData: function(result){
            return {
                "code": result.code, //解析接口状态
                "msg": result.msg, //解析提示文本
                "count": result.data.total, //解析数据长度
                "data": result.data.channels //解析数据列表
            };
        }
    });

    //头工具栏事件
    table.on('toolbar(data-table)', function(obj){
        var checks = table.checkStatus(obj.config.id);
        var data = checks.data;
        switch(obj.event){
            case 'search':
                var data =  getData();
                table.reload('data-table', {
                    url: 'promote/channelList',
                    where: data,
                    page: {
                        curr: 1
                    }
                });
                reloadData(data);
                break;
            case 'add':
                layer.open({
                    type: 2,
                    title: '推广渠道',
                    offset: '10%',
                    content: 'toEditChannel',
                    area: ['620px', '480px'],
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {     //确定按钮回调
                        var iframeWindow = window['layui-layer-iframe'+ index],
                            submit = layero.find('iframe').contents().find("#data-submit");

                        //监听提交
                        iframeWindow.layui.form.on('submit(data-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，静态更新表格中的数据
                            post('promote/addChannel', field, field, function (data) {
                                layer.msg("添加成功");
                                table.reload('data-table');
                                layer.close(index); //关闭弹层
                            });
                        });

                        submit.trigger('click');
                    }
                });
                break;
        };
    });

    //监听行工具事件
    table.on('tool(data-table)', function(obj){
        var data = obj.data;
        if(obj.event === 'edit'){
            layer.open({
                type: 2,
                title: '编辑渠道',
                offset: '10%',
                content: 'toEditChannel',
                area: ['620px', '480px'],
                btn: ['确定', '取消'],
                success: function(layero, index){   //弹窗成功后回调
                    var iframeWindow = window['layui-layer-iframe' + index];
                    $(iframeWindow.document).find('#id').val(data.id);   //传值到iframe页面
                },
                yes: function (index, layero) {     //确定按钮回调
                    var iframeWindow = window['layui-layer-iframe'+ index],
                        submit = layero.find('iframe').contents().find("#data-submit");

                    //监听提交
                    iframeWindow.layui.form.on('submit(data-submit)', function(data){
                        var field = data.field; //获取提交的字段

                        //提交 Ajax 成功后，静态更新表格中的数据
                        post('promote/editChannel', field, field, function (data) {
                            layer.msg("更新成功");
                            table.reload('data-table');
                            layer.close(index); //关闭弹层
                        });
                    });

                    submit.trigger('click');
                }
            });
        }else if(obj.event === 'restPwd'){
            layer.confirm('确定重置该渠道用户密码（重置后默认密码为：123456）', {offset: '30%'}, function(index){
                post('promote/restPwd', {'id': data.id}, false, function (data) {
                    layer.msg("密码重置成功");
                    layer.close(index);
                });
            });
        }
    });

    /**
     * 获取查询数据
     * @returns {{host: jQuery, online: jQuery, keyword: (*|jQuery|Array|string|undefined), status: (*|jQuery|Array|string|undefined)}}
     */
    function getData() {
        var keyword = $('#keyword').val();

        return {'keyword': keyword};
    }

    /**
     * 重新渲染数据
     * @param data
     */
    function reloadData(data) {
        $('#keyword').val(data.keyword);
    }

});